package com.topboya.linker.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.diyview.CenteredEditText;
import com.topboya.linker.model.OrgzitionManager;
import com.topboya.linker.model.SelectTeamBean;
import com.topboya.linker.model.levelitem.FollowUserGroup;
import com.topboya.linker.ui.adapter.FollowUserAdapter;
import com.topboya.linker.ui.adapter.RecycleViewDivider;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class SearchFollowUserActivity extends BaseActivity {

    private static final String TAG = "SearchFollowUserActivity:";
    private Toolbar toolbar;
    private TextView toolbar_title;
    private CenteredEditText search;
    private TextView tv_toolbar_right;
    private RecyclerView ry_team;
    private List<FollowUserGroup> followUserGroups;
//    用来保存组织架构的数据
    private List<FollowUserGroup> followUserGroupsOrg;
    private FollowUserAdapter followUserAdapter;

    private Button btn_selected;
    private TextView tv_selected_team_num;
//    用来保存被选中的随访人
    private ArrayList<FollowUserGroup> selectfollowuser;


    @Override
    protected int getLayoutId() {

        return R.layout.activity_search_team;
    }

    @Override
    protected void initView() {

        toolbar = findViewById(R.id.toolbar);
        toolbar_title = findViewById(R.id.toolbar_title);
        tv_toolbar_right = findViewById(R.id.tv_toolbar_right);
        ry_team = findViewById(R.id.ry_team);
        btn_selected = findViewById(R.id.btn_selected);
        tv_selected_team_num = findViewById(R.id.tv_selected_team_num);
        search = findViewById(R.id.search);
        initTitleView();
        initRycyclview();

    }

    private void initTitleView() {

        toolbar_title.setText("选择随访人");
        tv_toolbar_right.setVisibility(View.GONE);
    }

    private void initRycyclview() {

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        ry_team.setLayoutManager(layoutManager);
        ry_team.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, 2, 0xBFC0C2));
        followUserAdapter = new FollowUserAdapter(followUserGroups);
        ry_team.setAdapter(followUserAdapter);


    }

    @Override
    protected void initData() {

//        保存添加的随访人
        selectfollowuser = new ArrayList();
        selectfollowuser.clear();
        followUserGroups = new ArrayList<>();
        followUserGroupsOrg = OrgzitionManager.getIntanse().getfollowUserGroupsData();
        LogUtils.v(TAG + "followUserGroupsOrg:"+followUserGroupsOrg.size());
        ArrayList<FollowUserGroup> selectfollowuser = (ArrayList<FollowUserGroup>) getIntent().getSerializableExtra("selectfollowuser");
        if (null !=selectfollowuser){

            this.selectfollowuser.addAll(selectfollowuser);
        }
        tv_selected_team_num.setText("已选择："+ this.selectfollowuser.size());

    }

    @Override
    protected void initListener() {

        toolbar.setNavigationOnClickListener(v -> { back();});
        search.setOnEditorActionListener((v, actionId, event) ->{

            String searchKey =  v.getText().toString().trim();
            hideKeyboard(search);
            searchName(searchKey);
            return false;

        });

        followUserAdapter.setOnItemClickListener((adapter, view, position) -> {

           boolean isChekced= ((CheckBox)view.findViewById(R.id.ck_select)).isChecked();
           setSelectData(position,isChekced);


        });

        followUserAdapter.setOnItemChildClickListener((adapter, view, position) -> {

            boolean isChekced= ((CheckBox)view).isChecked();
            setSelectData(position,isChekced);


        });
        btn_selected.setOnClickListener(v -> { back(); });

    }


    public void setSelectData(int position,boolean isChecked){

        if (isChecked){

            selectfollowuser.add(followUserGroups.get(position));

        }else {
            selectfollowuser.remove(followUserGroups.get(position));
        }

        tv_selected_team_num.setText("已选择："+selectfollowuser.size()+"人");

    }
    public static void hideKeyboard(View view) {

        InputMethodManager manager = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.hideSoftInputFromWindow(view.getWindowToken(), 0);

    }

    private void searchName(String searchKey) {

        followUserGroups.clear();
        getAllSearchDepts(followUserGroupsOrg,followUserGroups,searchKey);

        followUserAdapter.setNewData(followUserGroups);
        LogUtils.v(TAG + "selectdepts:"+followUserGroups.toString());

    }

    public  void getAllSearchDepts(List<FollowUserGroup> departmentGroupBo, List<FollowUserGroup> selectdepts, String searchKey){

        if (null !=departmentGroupBo){

            for (FollowUserGroup departmentGroup: departmentGroupBo) {

                if (!departmentGroup.isSub() && departmentGroup.getName().contains(searchKey)){
                    selectdepts.add(departmentGroup);
                }
                getAllSearchDepts(departmentGroup.getSubGroupBosCd(),selectdepts,searchKey);

            }
        }


    }

    @Override
    protected BasePresenter createPresenter() {

        return null;
    }


    @Override
    public void onBackPressed() {

          back();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 是否触发按键为back键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        } else { // 如果不是back键正常响应
            return super.onKeyDown(keyCode, event);
        }
    }

    private void back() {

        Intent intent = new Intent();
        //把返回数据存入Intent
        intent.putExtra("selectfollowuser", selectfollowuser);
        //设置返回数据
        setResult(1002, intent);
        //关闭Activity
        finish();

    }


}
